<?php
function isLogged($json=NULL){
	// Initialize options for REST interface
	$url="/login/service/is-usuario-logado";

	//open connection
	$ch = curl_init();

	$fields_string = '';

	//url-ify the data for the POST
	foreach($json as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
	rtrim($fields_string, '&');

	//set the url, number of POST vars, POST data
	curl_setopt($ch,CURLOPT_URL, $url);
	curl_setopt($ch,CURLOPT_POST, count($json));
	curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
	// Will return the response, if false it print the response
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	//execute post
	$result = curl_exec($ch);

	//close connection
	curl_close($ch);

	// send request and wait for responce
  	$responce =  json_decode($result,true);

	return !!$responce;
}

?>

<?php

	class Usuario
	{
	    // valid property declarations:
   		private $usuario;
   		private $tipoUsuario;
   		private $id;

   		public function getTipoUsuario(){
   			return $this->tipoUsuario;
   		}

   		public function getUsuario(){
   			return $this->usuario;
   		}

   		public function getId(){
   			return $this->id;
   		}

   		public function setTipoUsuario($tipo){ $this->tipoUsuario = $tipo;}
   		public function setUsuario($usuario){ $this->usuario = $usuario;}


   		public function __toString(){
   			return "Usuario: ".$this->usuario;
   		}
	}
	//Inicia a sessão
	if ( ! session_id() ){

		@session_start();
    }

	if(!empty($_POST)){
		/*$logged = isLogged(array("id" => $_POST['id']));

        if($logged){*/
			$usuario = new Usuario();
			$usuario->setUsuario($_POST['user']);
			$usuario->setTipoUsuario($_POST['tipoUsuario']);
			$_SESSION['usuario'] = $usuario;


		/*}*/
		echo str_replace("\"", "", "/sistema");
	}
	
	if(!isset($_SESSION['usuario']) /*|| 
		!isLogged(array("id" => $_SESSION['usuario']->getId()))*/
	){ 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>LOGIN</title>
    <META http-equiv="refresh" content="0;URL=/sistema/usuario/login.html">
  </head>
  <body></body>
</html>

<?php 
	exit(0);
	}
?>